#include "scrollimage.hpp"
#include "global.hpp"

ScrollImage::ScrollImage(): xStart(0), yStart(0), xScroll(0), yScroll(0), repeatX(0), repeatY(0),
limitTop(0), limitBottom(0), limitLeft(0), limitRight(0), top(0), bottom(0), left(0), right(0)
{

}

ScrollImage::~ScrollImage()
{
}

bool ScrollImage::Load( const std::string& str )
{
	return image.LoadFullImage( str );
}

void ScrollImage::Stretch( float width, float height)
{
	image.Width(width);
	image.Height(height);
}

void ScrollImage::Render( float x, float y) const
{
	float xPosition = xStart-(x-idealX)*xScroll;
	float yPosition = yStart-(y-idealY)*yScroll;
	if( limitTop && yPosition < top )
		yPosition = top;
	else if( limitBottom && yPosition > bottom )
		yPosition = bottom;

	if( repeatX && repeatY )
	{
		while( xPosition > 0.0f )
		{
			xPosition -= image.Width();
		}
		while( xPosition < -image.Width() )
		{
			xPosition += image.Width();
		}
		while( yPosition > 0.0f )
		{
			yPosition -= image.Height();
		}
		while( yPosition < -image.Height() )
		{
			yPosition += image.Height();
		}
		for( ; yPosition < Global::Game::Height ; yPosition += image.Height() )
		{
			for( float xIndex= xPosition ; xIndex < Global::Game::Width ; xIndex += image.Width() )
			{
				image.Render( xIndex, yPosition );
			}
		}
	} else if( repeatX ) {
		while( xPosition > 0.0f )
		{
			xPosition -= image.Width();
		}
		while( xPosition < -image.Width() )
		{
			xPosition += image.Width();
		}
		image.Render( xPosition, yPosition );
		while( xPosition + image.Width() < Global::Game::Width )
		{
			xPosition += image.Width();
			image.Render( xPosition, yPosition );
		}
	} else if( repeatY ) {
		while( yPosition > 0.0f )
		{
			yPosition -= image.Height();
		}
		while( yPosition < -image.Height() )
		{
			yPosition += image.Height();
		}
		image.Render( xPosition, yPosition );
		while( yPosition + image.Height() < Global::Game::Height )
		{
			yPosition += image.Height();
			image.Render( xPosition, yPosition );
		}
	} else { // repeat both in a 2D grid
		image.Render( xPosition, yPosition );
	}
}

//void ScrollImage::Render()
//{
//	image.Render( xcurrent, ycurrent );
//}
//
//void ScrollImage::UpdateX( float screenMoved )
//{
//	xcurrent += screenMoved * xscroll;
//}
//
//void ScrollImage::UpdateY( float screenMoved )
//{
//	ycurrent += screenMoved * yscroll;
//}